﻿using System;
using System.Text;
using System.Windows.Forms;

namespace WordpressCodePlugin
{
    public partial class frmMain : Form
    {
        private PluginSettings m_settings;
        private string returnCode = "";
        public frmMain(PluginSettings settings)
        {
            InitializeComponent();
            m_settings = settings;
            SetToolTips();
            LoadOptions();
        }

        public string ReturnCode
        {
            get { return returnCode; }
        }

        public void LoadOptions()
        {
            cmbLanguage.SelectedIndex = cmbLanguage.FindString(m_settings.Language);
            chbxAutoLinks.Checked = m_settings.AutoLinks;
            chbxCollapse.Checked = m_settings.Collapse;
            chbxFirstLine.Checked = !string.IsNullOrEmpty(m_settings.FirstLine);
            txbxFirstLine.Text = m_settings.FirstLine;
            chbxGutter.Checked = m_settings.Gutter;
            chbxHighlight.Checked = !string.IsNullOrEmpty(m_settings.Highlight);
            txbxHighlight.Text = m_settings.Highlight;
            chbxHTMLScript.Checked = m_settings.HTMLScript;
            chbxLight.Checked = m_settings.Light;
            chbxPadLineNumbers.Checked = m_settings.PadLineNumbers;
            chbxToolbar.Checked = m_settings.Toolbars;
            chbxWrapLines.Checked = m_settings.WrapLines;
            txbxCode.Text = m_settings.Code;
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {            
            DialogResult = DialogResult.OK;
            Close();
        }              

        private void txbxCode_TextChanged(object sender, EventArgs e)
        {
            btnInsert.Enabled = !string.IsNullOrEmpty(txbxCode.Text);
        }

        private void lnkSetDefaults_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Save();
        }

        private void txbxFirstLine_TextChanged(object sender, EventArgs e)
        {
            chbxFirstLine.Checked = txbxFirstLine.Text.Length > 0;
        }

        private void txbxHighlight_TextChanged(object sender, EventArgs e)
        {
            chbxHighlight.Checked = txbxHighlight.Text.Length > 0;
        }

        private void SetToolTips()
        {
            ToolTip toolTip = new ToolTip()
            {
                AutoPopDelay = 5000,
                InitialDelay = 1000,
                ReshowDelay = 500,
                ShowAlways = true
            };

            toolTip.SetToolTip(chbxAutoLinks, "Makes all URLs in your posted code clickable. Defaults to true.");
            toolTip.SetToolTip(chbxCollapse, "If true, the code box will be collapsed when the page loads, requiring the visitor to click to expand it. Good for large code posts. Defaults to false.");
            toolTip.SetToolTip(chbxFirstLine, "Use this to change what number the line numbering starts at. It defaults to 1.");
            toolTip.SetToolTip(chbxGutter, "If false, the line numbering on the left side will be hidden. Defaults to true.");
            toolTip.SetToolTip(chbxHighlight, "Comma-seperated list of numbers — You can list the line numbers you want to be highlighted. For example \"4,7,19\"");
            toolTip.SetToolTip(chbxHTMLScript, "If true, any HTML/XML in your code will be highlighted. This is useful when you are mixing code into HTML, such as PHP inside of HTML. Defaults to false and will only work with certain code languages.");
            toolTip.SetToolTip(chbxLight, "If true, the gutter (line numbering) and toolbar will be hidden. This is helpful when posting only one or two lines of code. Defaults to false.");
            toolTip.SetToolTip(chbxPadLineNumbers, "Allows you to control the line number padding. true will result in automatic padding, false will result in no padding, and entering a number will force a specific amount of padding.");
            toolTip.SetToolTip(chbxToolbar, "If false, the toolbar containing the helpful buttons that appears when you hover over the code will not be shown. Defaults to true.");
            toolTip.SetToolTip(chbxWrapLines, "If true, line line wrapping will be disabled. This will cause a horizontal scrollbar to appear for long lines of code.");
        }

        public void Save()
        {
            m_settings.Language = cmbLanguage.SelectedItem.ToString();
            m_settings.AutoLinks = chbxAutoLinks.Checked;
            m_settings.Collapse = chbxCollapse.Checked;
            m_settings.FirstLine = chbxCollapse.Checked ? txbxFirstLine.Text : "";
            m_settings.Gutter = chbxGutter.Checked;
            m_settings.Highlight = chbxHighlight.Checked ? txbxHighlight.Text : "";
            m_settings.HTMLScript = chbxHTMLScript.Checked;
            m_settings.Light = chbxLight.Checked;
            m_settings.PadLineNumbers = chbxPadLineNumbers.Checked;
            m_settings.Toolbars = chbxToolbar.Checked;
            m_settings.WrapLines = chbxWrapLines.Checked;
        }

        public string FormatCode()
        {
            StringBuilder result = new StringBuilder();
            foreach (var line in txbxCode.Lines)
            {
                //result.AppendLine(line + "\n");
                result.AppendLine(line);
            }
            return result.ToString();
        }
    }
}
